{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6a62d3eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "#导包\n",
    "import  numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib as mpl\n",
    "import seaborn as sns\n",
    "%matplotlib inline\n",
    "plt.rcParams['font.family']=['SimHei']#显示中文标签\n",
    "plt.rcParams['axes.unicode_minus']=False#用来正常显示负号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1b8f239d",
   "metadata": {},
   "outputs": [],
   "source": [
    "data=pd.read_csv(r\"E:\\大三上\\数据分析与数据挖掘\\04\\小课\\数据源\\USER_INFO.csv\",encoding='gbk')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9ca8d0a0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 300309 entries, 0 to 300308\n",
      "Data columns (total 35 columns):\n",
      " #   Column                     Non-Null Count   Dtype  \n",
      "---  ------                     --------------   -----  \n",
      " 0   MONTH_ID                   300309 non-null  int64  \n",
      " 1   USER_ID                    300309 non-null  object \n",
      " 2   INNET_MONTH                300309 non-null  int64  \n",
      " 3   IS_AGREE                   300309 non-null  int64  \n",
      " 4   AGREE_EXP_DATE             152631 non-null  float64\n",
      " 5   CREDIT_LEVEL               300309 non-null  int64  \n",
      " 6   VIP_LVL                    210255 non-null  float64\n",
      " 7   ACCT_FEE                   300309 non-null  float64\n",
      " 8   CALL_DURA                  300309 non-null  int64  \n",
      " 9   NO_ROAM_LOCAL_CALL_DURA    300309 non-null  int64  \n",
      " 10  NO_ROAM_GN_LONG_CALL_DURA  300309 non-null  int64  \n",
      " 11  GN_ROAM_CALL_DURA          300309 non-null  int64  \n",
      " 12  CDR_NUM                    300309 non-null  int64  \n",
      " 13  NO_ROAM_CDR_NUM            300309 non-null  int64  \n",
      " 14  NO_ROAM_LOCAL_CDR_NUM      300309 non-null  int64  \n",
      " 15  NO_ROAM_GN_LONG_CDR_NUM    300309 non-null  int64  \n",
      " 16  GN_ROAM_CDR_NUM            300309 non-null  int64  \n",
      " 17  P2P_SMS_CNT_UP             300309 non-null  int64  \n",
      " 18  TOTAL_FLUX                 300309 non-null  float64\n",
      " 19  LOCAL_FLUX                 300309 non-null  float64\n",
      " 20  GN_ROAM_FLUX               300309 non-null  float64\n",
      " 21  CALL_DAYS                  300309 non-null  int64  \n",
      " 22  CALLING_DAYS               300309 non-null  int64  \n",
      " 23  CALLED_DAYS                300309 non-null  int64  \n",
      " 24  CALL_RING                  300309 non-null  int64  \n",
      " 25  CALLING_RING               300309 non-null  int64  \n",
      " 26  CALLED_RING                300309 non-null  int64  \n",
      " 27  CUST_SEX                   288867 non-null  float64\n",
      " 28  CERT_AGE                   288634 non-null  float64\n",
      " 29  CONSTELLATION_DESC         288634 non-null  object \n",
      " 30  MANU_NAME                  300307 non-null  object \n",
      " 31  MODEL_NAME                 300309 non-null  object \n",
      " 32  OS_DESC                    287936 non-null  object \n",
      " 33  TERM_TYPE                  300309 non-null  int64  \n",
      " 34  IS_LOST                    300309 non-null  float64\n",
      "dtypes: float64(9), int64(21), object(5)\n",
      "memory usage: 80.2+ MB\n"
     ]
    }
   ],
   "source": [
    "data.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "37793c34",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MONTH_ID                          0\n",
       "USER_ID                           0\n",
       "INNET_MONTH                       0\n",
       "IS_AGREE                          0\n",
       "AGREE_EXP_DATE               147678\n",
       "CREDIT_LEVEL                      0\n",
       "VIP_LVL                       90054\n",
       "ACCT_FEE                          0\n",
       "CALL_DURA                         0\n",
       "NO_ROAM_LOCAL_CALL_DURA           0\n",
       "NO_ROAM_GN_LONG_CALL_DURA         0\n",
       "GN_ROAM_CALL_DURA                 0\n",
       "CDR_NUM                           0\n",
       "NO_ROAM_CDR_NUM                   0\n",
       "NO_ROAM_LOCAL_CDR_NUM             0\n",
       "NO_ROAM_GN_LONG_CDR_NUM           0\n",
       "GN_ROAM_CDR_NUM                   0\n",
       "P2P_SMS_CNT_UP                    0\n",
       "TOTAL_FLUX                        0\n",
       "LOCAL_FLUX                        0\n",
       "GN_ROAM_FLUX                      0\n",
       "CALL_DAYS                         0\n",
       "CALLING_DAYS                      0\n",
       "CALLED_DAYS                       0\n",
       "CALL_RING                         0\n",
       "CALLING_RING                      0\n",
       "CALLED_RING                       0\n",
       "CUST_SEX                      11442\n",
       "CERT_AGE                      11675\n",
       "CONSTELLATION_DESC            11675\n",
       "MANU_NAME                         2\n",
       "MODEL_NAME                        0\n",
       "OS_DESC                       12373\n",
       "TERM_TYPE                         0\n",
       "IS_LOST                           0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.isnull().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "517b6923",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "cfbb7ede",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "25.0\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "\n",
    "def newton_interpolation(x_points, y_points, target_x):\n",
    "    n = len(x_points)\n",
    "    # 初始化差商表\n",
    "    divided_diff = np.zeros((n, n))\n",
    "    if len(y_points) != n:\n",
    "        raise ValueError('x_points and y_points must have the same length')\n",
    "    # 第 0 列初始化为 y_points\n",
    "    divided_diff[:, 0] = y_points\n",
    "    # 计算 i 阶差商\n",
    "    for i in range(1, n):\n",
    "        for j in range(n - i):\n",
    "            divided_diff[j, i] = (divided_diff[j + 1, i - 1] - divided_diff[j, i - 1]) / (x_points[j + i] - x_points[j])\n",
    "\n",
    "    # 根据差商计算插值\n",
    "    result = y_points[0]\n",
    "    for i in range(1, n):\n",
    "        # 第 i 阶差商\n",
    "        p = divided_diff[0, i]\n",
    "        # 计算 x-x_j,将所有的结果相乘\n",
    "        for j in range(i):\n",
    "            p *= (target_x - x_points[j])\n",
    "        result += p\n",
    "    return result\n",
    "\n",
    "\n",
    "# 测试验证\n",
    "x_points = [1, 2, 3, 4]\n",
    "y_points = [1, 4, 9, 16]\n",
    "print(newton_interpolation(x_points, y_points, 5))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22ff0c9d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
